using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_ArticleList : PagedControl
{
    ArticleDAO daoArticle = new ArticleDAO();
    public override string RequestString
    {
        get {
            if (Request["search"] != null) return "Default.aspx?ctrl=article_list&search=" + Request["search"] + "&page=";
            return "Default.aspx?ctrl=article_list&page=";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        p = Request["page"];      
        totals = 0;
        currentPage = GetPageNumber(p);
        ActiveAccording = "article";
        if (Request["search"] == "" || Request["search"] == null)        {
           
            {
                LoadData();
                lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
            }
        }
        else
        {        
            {              
                dgrArticle.DataSource = daoArticle.GetAllArticlesExt(pageSize, currentPage, ref totals, "%" + Request["search"] + "%",ServiceObject.Both);
                dgrArticle.DataBind();
                lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
            }
        }
        
    }
    public void LoadData()
    {
        dgrArticle.DataSource = daoArticle.GetAllArticles(pageSize, currentPage,ref totals,ServiceObject.Both);
        dgrArticle.DataBind();
    }
    public void LoadData(string keyword)
    {
        //dgrArticle.DataSource=
    }
    protected void dgrArticle_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = dgrArticle.DataKeys[e.NewEditIndex].Value.ToString();
        Response.Redirect("Default.aspx?ctrl=article&mode=1&id="+id);

    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
       
        int count = 0;
        bool rs = true;
        foreach (GridViewRow row in dgrArticle.Rows)
        {            
            bool check=((CheckBox)(row.FindControl("chkChoice"))).Checked;
            if (check)
            {
                ((CheckBox)(row.FindControl("chkChoice"))).Checked = false;
                count++;
                int id = int.Parse(dgrArticle.DataKeys[row.RowIndex].Value.ToString());
                if (daoArticle.Delete(id) <= 0)
                {
                    rs = false;
                    break;
                }
            }
        }
        if (count == 0) ShowMessage("Please specify item to delete");
        else if (rs) ShowMessage("Selected items are deleted successfully");
        else ShowMessage("Error occured. Some items may not be deleted");
        LoadData();
    
    }
   // protected void lnkSearch_Click(object sender, EventArgs e)
   //{
   //    //Response.Redirect("Default.aspx?ctrl=article_list&search="+txtSearch.Text);  
     
   //        dgrArticle.DataSource = daoArticle.GetAllArticlesExt(pageSize, currentPage, ref totals, "%" + txtSearch.Text + "%");
   //        dgrArticle.DataBind();
   //        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
       
   //}
    //public string Keyword()
    //{
    //    return txtSearch.Text;
    //}
}
